package com.xcf.controller.business.common;

import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xcf.controller.base.BaseController;
import com.xcf.service.business.common.internaltradebase.InternalTradeBaseService;
import com.xcf.util.Const;
import com.xcf.util.FileUpload;
import com.xcf.util.ObjectExcelRead;
import com.xcf.util.PageData;
import com.xcf.util.PathUtil;
import com.xcf.util.ResponseResult;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
@Controller
@RequestMapping(value="/internalTradeBase")
public class InternalTradeBaseController extends BaseController{
	@Resource
	private InternalTradeBaseService internalTradeBaseService;
	/**
	 * 跳转列表
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/list")
	public ModelAndView internalTradeBasePage()throws Exception{
		ModelAndView view = new	ModelAndView();
		view.setViewName("/business/common/internaltradeBase/internal_trade_base_list");
		return view;
		
	}
	/**
	 * 数据查询
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/internalTradeBaseInfo")
	@ResponseBody
	public ResponseResult internalTradeBaseInfo(int page) throws Exception{
		PageHelper.startPage(page, 10);
		PageData pd = new PageData();
		pd = this.getPageData();
		if(pd.getString("GOODS_NAME")!=null && pd.getString("GOODS_NAME")!=""){
			String GOODS_NAME= URLDecoder.decode(pd.getString("GOODS_NAME"), "utf-8");
			pd.put("GOODS_NAME", GOODS_NAME);
		}
		if(pd.getString("SPELL_CODE")!=null && pd.getString("SPELL_CODE")!=""){
			String SPELL_CODE= URLDecoder.decode(pd.getString("SPELL_CODE"), "utf-8");
			pd.put("SPELL_CODE", SPELL_CODE);
		}
		if(pd.getString("BOX_TYPE")!=null && pd.getString("BOX_TYPE")!=""){
			String BOX_TYPE= URLDecoder.decode(pd.getString("BOX_TYPE"), "utf-8");
			pd.put("BOX_TYPE", BOX_TYPE);
		}
		List<Map> list = internalTradeBaseService.findInfo(pd);

		return ResponseResult.success(new PageInfo(list));
	}
	/**
	 * 保存信息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/saveInternalTradeBaseInfo")
	@ResponseBody
	public String saveInternalTradeBaseInfo()throws Exception{
		PageData pd = new PageData();
		pd=this.getPageData();
		if(pd.getString("ENABLE")!=null && pd.getString("ENABLE")!=""){
		if(pd.getString("ENABLE").equals("是")){
			pd.put("ENABLE", 1);
		}else{
			pd.put("ENABLE", 0);
		}
		}else{
			pd.put("ENABLE", 0);
		}
		if(pd.getString("ID").isEmpty()){
			
			internalTradeBaseService.saveInfo(pd);
		}else{
			internalTradeBaseService.updateInfo(pd);
		}
		return null;
		
	}
	/**
	 * 删除支付单位信息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/deleteInfo")
	@ResponseBody
	public String deleteInfo()throws Exception{
		PageData pd = new PageData();
		pd = this.getPageData();

		String data = pd.getString("array");

		JSONArray items = JSONArray.fromObject(data);
		for(int i=0,size=items.size();i<size;i++){
			JSONObject item = items.getJSONObject(i);
			String id = item.getString("id").trim();
			PageData pdq = new PageData();
			pdq.put("ID", id);
			internalTradeBaseService.deletInfo(pdq);
		}
		return null;
		
	}
	
	/**
	 * 导入
	 * @param file
	 * @return
	 * @throws Exception
	 */
		@RequestMapping(value="/internalTrade")
		@ResponseBody
		public Object internalTrade(@RequestParam("file") CommonsMultipartFile file) throws Exception {
			String filePath = PathUtil.getClasspath() + Const.FILEPATHFILE;
			String fileName =  FileUpload.fileUp(file, filePath, "test");	
			List<PageData> listPd = (List)ObjectExcelRead.readExcel(filePath, fileName, 2, 0, 0);
			PageData pdq = new PageData();
			for(PageData pd : listPd){
				if(pd.getString("var0")!=null && pd.getString("var0")!="" && pd.getString("var1")!=null && pd.getString("var1")!="" &&
						pd.getString("var3")!=null && pd.getString("var3")!="" && pd.getString("var8")!=null && pd.getString("var8")!=""&&
						pd.getString("var9")!=null && pd.getString("var9")!=""){
					PageData pdq1 = new PageData();
					pdq1.put("GOODS_NAME", pd.getString("var0"));
					pdq1.put("GOODS_CATEGORY_NAME", pd.getString("var1"));
					pdq1.put("ORGIN_PORT_END_MILEAGE", pd.getString("var3"));
					pdq1.put("DEST_PORT_END_MILEAGE", pd.getString("var8"));
					pdq1.put("BOX_TYPE", pd.getString("var9"));
					PageData pdq2 = internalTradeBaseService.findInfoByInfo(pdq1);
					if(pdq2!=null){
						pdq.put("GOODS_NAME", pd.getString("var0"));
						pdq.put("GOODS_CATEGORY_NAME", pd.getString("var1"));
						
						if(pd.getString("var2")!=null&&pd.getString("var2")!=""){
							int i = Integer.parseInt(pd.getString("var2"));
							pdq.put("ORGIN_PORT_START_MILEAGE", i);
						}
						if(pd.getString("var3")!=null&&pd.getString("var3")!=""){
							int i = Integer.parseInt(pd.getString("var3"));
							pdq.put("ORGIN_PORT_END_MILEAGE", i);
						}
						if(pd.getString("var4")!=null&&pd.getString("var4")!=""){
							float i = Float.parseFloat(pd.getString("var4"));
							pdq.put("DOMESTIC_TRADE_PRICE", i);
						}
						if(pd.getString("var5")!=null&&pd.getString("var5")!=""){
							float i = Float.parseFloat(pd.getString("var5"));
							pdq.put("FOREIGN_TRADE_PRICE", i);
						}
						if(pd.getString("var6")!=null&&pd.getString("var6")!=""){
							float i = Float.parseFloat(pd.getString("var6"));
							pdq.put("EMPTY_CONTAINER_PRICE", i);
						}
						if(pd.getString("var7")!=null&&pd.getString("var7")!=""){
							int i = Integer.parseInt(pd.getString("var7"));
							pdq.put("DEST_PORT_BEGIN_MILEAGE", i);
						}
						if(pd.getString("var8")!=null&&pd.getString("var8")!=""){
							int i = Integer.parseInt(pd.getString("var8"));
							pdq.put("DEST_PORT_END_MILEAGE", i);
						}
						pdq.put("BOX_TYPE", pd.getString("var9"));
						pdq.put("ENABLE", pd.getString("var10"));
						pdq.put("SPELL_CODE", pd.getString("var11"));
						pdq.put("REMARK", pd.getString("var12"));
						
						
						if(pd.getString("var13")!=null&&pd.getString("var13")!=""){
							SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
							String dstr=pd.getString("var13");  
							java.util.Date date=sdf.parse(dstr);
							pdq.put("START_PRICE_EXP_DATE", date);
						}
						if(pd.getString("var14")!=null&&pd.getString("var14")!=""){
							SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
							String dstr=pd.getString("var14");  
							java.util.Date date=sdf.parse(dstr);
							pdq.put("END_PRICE_EXP_DATE", date);
						}
						internalTradeBaseService.updateInfoByInfo(pdq);
					}else{
						pdq.put("GOODS_NAME", pd.getString("var0"));
						pdq.put("GOODS_CATEGORY_NAME", pd.getString("var1"));
//						pdq.put("ORGIN_PORT_START_MILEAGE", pd.getString("var2"));
//						pdq.put("ORGIN_PORT_END_MILEAGE", pd.getString("var3"));
//						pdq.put("DOMESTIC_TRADE_PRICE", pd.getString("var4"));
//						pdq.put("FOREIGN_TRADE_PRICE", pd.getString("var5"));
//						pdq.put("EMPTY_CONTAINER_PRICE", pd.getString("var6"));
//						pdq.put("DEST_PORT_BEGIN_MILEAGE", pd.getString("var7"));
//						pdq.put("DEST_PORT_END_MILEAGE", pd.getString("var8"));
						if(pd.getString("var2")!=null&&pd.getString("var2")!=""){
							int i = Integer.parseInt(pd.getString("var2"));
							pdq.put("ORGIN_PORT_START_MILEAGE", i);
						}
						if(pd.getString("var3")!=null&&pd.getString("var3")!=""){
							int i = Integer.parseInt(pd.getString("var3"));
							pdq.put("ORGIN_PORT_END_MILEAGE", i);
						}
						if(pd.getString("var4")!=null&&pd.getString("var4")!=""){
							float i = Float.parseFloat(pd.getString("var4"));
							pdq.put("DOMESTIC_TRADE_PRICE", i);
						}
						if(pd.getString("var5")!=null&&pd.getString("var5")!=""){
							float i = Float.parseFloat(pd.getString("var5"));
							pdq.put("FOREIGN_TRADE_PRICE", i);
						}
						if(pd.getString("var6")!=null&&pd.getString("var6")!=""){
							float i = Float.parseFloat(pd.getString("var6"));
							pdq.put("EMPTY_CONTAINER_PRICE", i);
						}
						if(pd.getString("var7")!=null&&pd.getString("var7")!=""){
							int i = Integer.parseInt(pd.getString("var7"));
							pdq.put("DEST_PORT_BEGIN_MILEAGE", i);
						}
						if(pd.getString("var8")!=null&&pd.getString("var8")!=""){
							int i = Integer.parseInt(pd.getString("var8"));
							pdq.put("DEST_PORT_END_MILEAGE", i);
						}
						pdq.put("BOX_TYPE", pd.getString("var9"));
						pdq.put("ENABLE", pd.getString("var10"));
						pdq.put("SPELL_CODE", pd.getString("var11"));
						pdq.put("REMARK", pd.getString("var12"));
						if(pd.getString("var13")!=null&&pd.getString("var13")!=""){
							SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
							String dstr=pd.getString("var13");  
							java.util.Date date=sdf.parse(dstr);
							pdq.put("START_PRICE_EXP_DATE", date);
						}
						if(pd.getString("var14")!=null&&pd.getString("var14")!=""){
							SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
							String dstr=pd.getString("var14");  
							java.util.Date date=sdf.parse(dstr);
							pdq.put("END_PRICE_EXP_DATE", date);
						}
						internalTradeBaseService.saveInfo(pdq);
					}
				}
				
			}
			return ResponseResult.success(listPd);
			
		}
}
